package com.lzy.business.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.SysUserAdminServiceApi;
import com.lzy.base.dtos.SysUserAdminDto;
import com.lzy.base.service.SysUserAdminService;
import com.lzy.coupon.apis.MotorcycleServiceApi;
import com.lzy.coupon.apis.MotorcycleSpecServiceApi;
import com.lzy.coupon.dtos.MotorcycleDto;
import com.lzy.coupon.dtos.MotorcycleSpecDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.member.apis.MemberUserServiceApi;
import com.lzy.member.dtos.MemberUserDto;
import com.lzy.product.apis.*;
import com.lzy.product.common.util.UserUtil;
import com.lzy.product.dtos.ProjectDto;
import com.lzy.product.dtos.agent.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by WIN7 on 2018/4/13.
 */
@Component
public class AgentProtocolFacade {

    @Autowired
    private AgentProtocolServiceApi agentProtocolServiceApi;
    @Autowired
    private AgentProtocolImageTextServiceApi agentProtocolImageTextServiceApi;
    @Autowired
    private AgentProductProjectServiceApi agentProductProjectServiceApi;
    @Autowired
    private AgentProtocolYieldRatioServiceApi agentProtocolYieldRatioServiceApi;
    @Autowired
    private AgentProtocolProductServiceApi agentProtocolProductServiceApi;
    @Autowired
    private AgentProtocolSystemServiceApi agentProtocolSystemServiceApi;
    @Autowired
    private AgentProtocolCommissionServiceApi agentProtocolCommissionServiceApi;
    @Autowired
    private AgentPromoteServiceApi agentPromoteServiceApi;
    @Autowired
    private ProjectServiceApi projectServiceApi;
    @Autowired
    private MotorcycleSpecServiceApi motorcycleSpecServiceApi;
    @Autowired
    private MotorcycleServiceApi motorcycleServiceApi;

    /**
     * 新增协议
     *
     * @param dto
     * @return
     */
    public Boolean createAagntProtocol(AgentProtocolInfoDto dto) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updateTime = myFmt.format(new Date());
        AgentProtocolDto apd = dto.getAgentProtocolDto();
        apd.setCreateTime(updateTime);
        apd.setUpdateTime(updateTime);
        apd.setStatus(1);
        apd.setCreateUser(UserUtil.getCurrentUser().getId());
        if(null == apd.getPrice() || "".equals(apd.getPrice())){
            apd.setPrice(new BigDecimal("0"));
        }
        Long protocolId = agentProtocolServiceApi.insert(dto.getAgentProtocolDto());
        if (null != protocolId && protocolId != 0) {
            //图文详情
            if (null != dto.getAgentProtocolImageTextDtoList() && !dto.getAgentProtocolImageTextDtoList().isEmpty()) {
                for (AgentProtocolImageTextDto agentProtocolImageTextDto : dto.getAgentProtocolImageTextDtoList()) {
                    agentProtocolImageTextDto.setProtocolId(protocolId);
                    agentProtocolImageTextDto.setCreateTime(updateTime);
                    agentProtocolImageTextDto.setUpdateTime(updateTime);
                    agentProtocolImageTextServiceApi.insert(agentProtocolImageTextDto);
                }
            }
            //产品关联
            if (null != dto.getAgentProtocolProductDtoList() && !dto.getAgentProtocolProductDtoList().isEmpty()) {
                for (AgentProtocolProductDto agentProtocolProductDto : dto.getAgentProtocolProductDtoList()) {
                    agentProtocolProductDto.setProtocolId(protocolId);
                    agentProtocolProductServiceApi.insert(agentProtocolProductDto);
                }
            }
            //体系关联
            if (null != dto.getAgentProtocolSystemDtoList() && !dto.getAgentProtocolSystemDtoList().isEmpty()) {
                for (AgentProtocolSystemDto agentProtocolSystemDto : dto.getAgentProtocolSystemDtoList()) {
                    agentProtocolSystemDto.setProtocolId(protocolId);
                    agentProtocolSystemServiceApi.insert(agentProtocolSystemDto);
                }
            }
            //协议额外赠送
            if (null != dto.getAgentProductProjectDtoList() && !dto.getAgentProductProjectDtoList().isEmpty()) {
                for (AgentProductProjectDto agentProductProjectDto : dto.getAgentProductProjectDtoList()) {
                    if(null != agentProductProjectDto.getProjectId()){
                        agentProductProjectDto.setSerialId(protocolId);
                        agentProductProjectDto.setModel(2);
                        if(null != agentProductProjectDto.getSpecId()){
                            DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
                            MotorcycleSpecDto motorcycleSpecDto = motorcycleSpecServiceApi.selectBySpecId(agentProductProjectDto.getSpecId());
                            agentProductProjectDto.setPrice(motorcycleSpecDto.getPrice());
                            DataSourceHolder.clearDataSource();
                        }
                        agentProductProjectServiceApi.insert(agentProductProjectDto);
                    }
                }
            }
            //返现
            if (null != dto.getAgentProtocolYieldRatioDtoList() && !dto.getAgentProtocolYieldRatioDtoList().isEmpty()) {
                for (AgentProtocolYieldRatioDto agentProtocolYieldRatioDto : dto.getAgentProtocolYieldRatioDtoList()) {
                    if(null != agentProtocolYieldRatioDto.getCashbackRatio() && !"".equals(agentProtocolYieldRatioDto.getCashbackRatio())){
                        if(null != agentProtocolYieldRatioDto.getCashbackTime() && !"".equals(agentProtocolYieldRatioDto.getCashbackTime())){
                            if(null != agentProtocolYieldRatioDto.getCashbackWithdrawal() && !"".equals(agentProtocolYieldRatioDto.getCashbackWithdrawal())) {
                                agentProtocolYieldRatioDto.setProtocolId(protocolId);
                                agentProtocolYieldRatioServiceApi.insert(agentProtocolYieldRatioDto);
                            }
                        }
                    }
                }
            }
            //分拥
            if (null != dto.getAgentProtocolCommissionDtoList() && !dto.getAgentProtocolCommissionDtoList().isEmpty()) {
                for (AgentProtocolCommissionDto agentProtocolCommissionDto : dto.getAgentProtocolCommissionDtoList()) {
                    agentProtocolCommissionDto.setProtocolId(protocolId);
                    agentProtocolCommissionServiceApi.insert(agentProtocolCommissionDto);
                }
            }
            //升级分拥
            if (null != dto.getAgentPromoteCommissionDtoList() && !dto.getAgentPromoteCommissionDtoList().isEmpty()) {
                for (AgentPromoteCommissionDto agentPromoteCommissionDto : dto.getAgentPromoteCommissionDtoList()) {
                    AgentPromoteDto agentPromoteDto = agentPromoteCommissionDto.getAgentPromoteDto();
                    List<AgentProtocolCommissionDto> agentProtocolCommissionDtoList = agentPromoteCommissionDto.getAgentProtocolCommissionDtoList();
                    if(null != agentProtocolCommissionDtoList && agentProtocolCommissionDtoList.size()>0){
                        agentPromoteDto.setProtocolId(protocolId);
                        Long promoteId = agentPromoteServiceApi.insert(agentPromoteDto);
                        for(AgentProtocolCommissionDto agentProtocolCommissionDto:agentProtocolCommissionDtoList){
                            agentProtocolCommissionDto.setPromoteId(promoteId);
                            agentProtocolCommissionDto.setProtocolId(protocolId);
                            agentProtocolCommissionServiceApi.insert(agentProtocolCommissionDto);
                        }
                    }
                }
            }
        }
        return true;
    }

    /**
     * 协议列表
     *
     * @param pager
     * @param agentProtocolDto
     * @return
     */
    public PageInfo<AgentProtocolDto> agentProtocolList(Pager pager, AgentProtocolDto agentProtocolDto) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        PageInfo pageInfo = agentProtocolServiceApi.selectList(pager, agentProtocolDto);
        return pageInfo;
    }

    /**
     * 更新协议状态
     *
     * @param agentProtocolDto
     * @return
     */
    public Boolean updateAgentProtocolStatus(AgentProtocolDto agentProtocolDto) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        if(null != agentProtocolDto.getDeleted()){
            agentProtocolProductServiceApi.deleteByProtocolId(agentProtocolDto.getId());
        }
        return agentProtocolServiceApi.update(agentProtocolDto);
    }

    /**
     * 获取协议详情
     *
     * @param id
     * @return
     */
    public AgentProtocolInfoDto getAgentProtocolInfo(Long id) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        //协议详情
        AgentProtocolDto agentProtocolDto = agentProtocolServiceApi.selectDetailById(id);
        //图文详情
        List<AgentProtocolImageTextDto> agentProtocolImageTextDtoList = agentProtocolImageTextServiceApi.selectByProtocolId(id);
        //产品
        List<AgentProtocolProductDto> agentProtocolProductDtoList = agentProtocolProductServiceApi.selectByProtocolId(id);
        //体系
        List<AgentProtocolSystemDto> agentProtocolSystemDtoList = agentProtocolSystemServiceApi.selectByProtocolId(id);
        //返现
        List<AgentProtocolYieldRatioDto> agentProtocolYieldRatioDtoList = agentProtocolYieldRatioServiceApi.selectByProtocolId(id);
        //分拥
        List<AgentProtocolCommissionDto> agentProtocolCommissionDtoList = agentProtocolCommissionServiceApi.selectByProtocolId(id);
        //代理商产品包含项
        List<AgentProductProjectDto> agentProductProjectDtoList = agentProductProjectServiceApi.selectByProtocolId(id,2);
        for (AgentProductProjectDto agentProductProjectDto : agentProductProjectDtoList) {
            if(null != agentProductProjectDto.getProjectId()) {
                ProjectDto projectDto = projectServiceApi.getById(agentProductProjectDto.getProjectId());
                if (null != agentProductProjectDto.getSpecId()) {
                    DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
                    MotorcycleSpecDto motorcycleSpecDto = motorcycleSpecServiceApi.selectBySpecId(agentProductProjectDto.getSpecId());
                    MotorcycleDto motorcycleDto = motorcycleServiceApi.findById(motorcycleSpecDto.getMotorcycleId());
                    AgentProductMotorcycleDto agentProductMotorcycleDto = new AgentProductMotorcycleDto();
                    agentProductMotorcycleDto.setMotorcycleId(motorcycleSpecDto.getId());
                    agentProductMotorcycleDto.setMotorcycleName(motorcycleSpecDto.getPrice() + "----" + motorcycleDto.getName());
                    agentProductProjectDto.setAgentProductMotorcycleDto(agentProductMotorcycleDto);
                    DataSourceHolder.clearDataSource();
                }
                agentProductProjectDto.setProjectDto(projectDto);
            }
        }
        //返回数据
        AgentProtocolInfoDto agentProtocolInfoDto = new AgentProtocolInfoDto();
        agentProtocolInfoDto.setAgentProtocolDto(agentProtocolDto);
        agentProtocolInfoDto.setAgentProtocolImageTextDtoList(agentProtocolImageTextDtoList);
        agentProtocolInfoDto.setAgentProtocolProductDtoList(agentProtocolProductDtoList);
        agentProtocolInfoDto.setAgentProtocolSystemDtoList(agentProtocolSystemDtoList);
        agentProtocolInfoDto.setAgentProtocolYieldRatioDtoList(agentProtocolYieldRatioDtoList);
        agentProtocolInfoDto.setAgentProtocolCommissionDtoList(agentProtocolCommissionDtoList);
        agentProtocolInfoDto.setAgentProductProjectDtoList(agentProductProjectDtoList);
        return agentProtocolInfoDto;
    }

    /**
     * 更新协议
     *
     * @param dto
     * @return
     */
    public Boolean updateAgentProtocol(AgentProtocolInfoDto dto) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updateTime = myFmt.format(new Date());
        AgentProtocolDto apd = dto.getAgentProtocolDto();
        apd.setUpdateTime(updateTime);
        apd.setUpdateUser(UserUtil.getCurrentUser().getId());
        if(null == apd.getPrice() || "".equals(apd.getPrice())){
            apd.setPrice(new BigDecimal("0"));
        }
        //更新协议主表
        agentProtocolServiceApi.update(apd);
        //更新图文详情表
        agentProtocolImageTextServiceApi.deleteByProtocolId(apd.getId());
        if (null != dto.getAgentProtocolImageTextDtoList() && !dto.getAgentProtocolImageTextDtoList().isEmpty()) {
            for (AgentProtocolImageTextDto agentProtocolImageTextDto : dto.getAgentProtocolImageTextDtoList()) {
                agentProtocolImageTextDto.setProtocolId(apd.getId());
                agentProtocolImageTextDto.setCreateTime(updateTime);
                agentProtocolImageTextDto.setUpdateTime(updateTime);
                agentProtocolImageTextServiceApi.insert(agentProtocolImageTextDto);
            }
        }
        //产品关联
        agentProtocolProductServiceApi.deleteByProtocolId(apd.getId());
        if (null != dto.getAgentProtocolProductDtoList() && !dto.getAgentProtocolProductDtoList().isEmpty()) {
            for (AgentProtocolProductDto agentProtocolProductDto : dto.getAgentProtocolProductDtoList()) {
                agentProtocolProductDto.setProtocolId(apd.getId());
                agentProtocolProductServiceApi.insert(agentProtocolProductDto);
            }
        }
        //体系关联
        agentProtocolSystemServiceApi.deleteByProtocolId(apd.getId());
        if (null != dto.getAgentProtocolSystemDtoList() && !dto.getAgentProtocolSystemDtoList().isEmpty()) {
            for (AgentProtocolSystemDto agentProtocolSystemDto : dto.getAgentProtocolSystemDtoList()) {
                agentProtocolSystemDto.setProtocolId(apd.getId());
                agentProtocolSystemServiceApi.insert(agentProtocolSystemDto);
            }
        }
        //协议额外赠送
        agentProductProjectServiceApi.deleteByProtocolId(apd.getId(),2);
        if (null != dto.getAgentProductProjectDtoList() && !dto.getAgentProductProjectDtoList().isEmpty()) {
            for (AgentProductProjectDto agentProductProjectDto : dto.getAgentProductProjectDtoList()) {
                if (null != agentProductProjectDto.getProjectId() && !"".equals(agentProductProjectDto.getProjectId())) {
                    agentProductProjectDto.setSerialId(apd.getId());
                    agentProductProjectDto.setModel(2);
                    if (null != agentProductProjectDto.getSpecId()) {
                        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
                        MotorcycleSpecDto motorcycleSpecDto = motorcycleSpecServiceApi.selectBySpecId(agentProductProjectDto.getSpecId());
                        agentProductProjectDto.setPrice(motorcycleSpecDto.getPrice());
                        DataSourceHolder.clearDataSource();
                    }
                    agentProductProjectServiceApi.insert(agentProductProjectDto);
                }
            }
        }
        //返现
        agentProtocolYieldRatioServiceApi.deleteByProtocolId(apd.getId());
        if (null != dto.getAgentProtocolYieldRatioDtoList() && !dto.getAgentProtocolYieldRatioDtoList().isEmpty()) {
            for (AgentProtocolYieldRatioDto agentProtocolYieldRatioDto : dto.getAgentProtocolYieldRatioDtoList()) {
                if(null != agentProtocolYieldRatioDto.getCashbackRatio() && !"".equals(agentProtocolYieldRatioDto.getCashbackRatio())){
                    if(null != agentProtocolYieldRatioDto.getCashbackTime() && !"".equals(agentProtocolYieldRatioDto.getCashbackTime())){
                        if(null != agentProtocolYieldRatioDto.getCashbackWithdrawal() && !"".equals(agentProtocolYieldRatioDto.getCashbackWithdrawal())){
                            agentProtocolYieldRatioDto.setProtocolId(apd.getId());
                            agentProtocolYieldRatioServiceApi.insert(agentProtocolYieldRatioDto);
                        }
                    }
                }
            }
        }
        //分拥
        agentProtocolCommissionServiceApi.deleteByProtocolId(apd.getId());
        if (null != dto.getAgentProtocolCommissionDtoList() && !dto.getAgentProtocolCommissionDtoList().isEmpty()) {
            for (AgentProtocolCommissionDto agentProtocolCommissionDto : dto.getAgentProtocolCommissionDtoList()) {
                agentProtocolCommissionDto.setProtocolId(apd.getId());
                agentProtocolCommissionServiceApi.insert(agentProtocolCommissionDto);
            }
        }
        //升级分拥
        agentPromoteServiceApi.deleteByProtocolId(apd.getId());
        if (null != dto.getAgentPromoteCommissionDtoList() && !dto.getAgentPromoteCommissionDtoList().isEmpty()) {
            for (AgentPromoteCommissionDto agentPromoteCommissionDto : dto.getAgentPromoteCommissionDtoList()) {
                AgentPromoteDto agentPromoteDto = agentPromoteCommissionDto.getAgentPromoteDto();
                List<AgentProtocolCommissionDto> agentProtocolCommissionDtoList = agentPromoteCommissionDto.getAgentProtocolCommissionDtoList();
                if(null != agentProtocolCommissionDtoList && agentProtocolCommissionDtoList.size()>0) {
                    agentPromoteDto.setProtocolId(apd.getId());
                    Long promoteId = agentPromoteServiceApi.insert(agentPromoteDto);
                    for (AgentProtocolCommissionDto agentProtocolCommissionDto : agentProtocolCommissionDtoList) {
                        agentProtocolCommissionDto.setPromoteId(promoteId);
                        agentProtocolCommissionDto.setProtocolId(apd.getId());
                        agentProtocolCommissionServiceApi.insert(agentProtocolCommissionDto);
                    }
                }
            }
        }
        return true;
    }

}
